var $multitabs;
$(function() {
	checkLogin();
	// 滚动条
	const ps = new PerfectScrollbar('.lyear-layout-sidebar-scroll', {
		swipeEasing: false,
		suppressScrollX: true
	});

	// 侧边栏
	$(".lyear-aside-toggler").bind('click', function() {
		$('.lyear-layout-sidebar').toggleClass('lyear-aside-open');
		$("body").toggleClass('lyear-layout-sidebar-close');

		if ($('.lyear-mask-modal').length == 0) {
			$('<div class="lyear-mask-modal"></div>').prependTo('body');
		} else {
			$('.lyear-mask-modal').remove();
		}
		$('.lyear-mask-modal').on('click', function() {
			$(this).remove();
			$('.lyear-layout-sidebar').toggleClass('lyear-aside-open');
			$('body').toggleClass('lyear-layout-sidebar-close');
		});
	});


	loadMenus();
	// 侧边栏导航
	// initMenus();
	// 选项卡 API文档 https://gitee.com/edwinhuish/multi-tabs
	$multitabs = $('#iframe-content').multitabs({
		iframe: true,
		//主框架样式
		class: "container-fluid p-t-15",
		nav: {
			backgroundColor: '#ffffff',
			maxTabs: 35, // 选项卡最大值
		},
		init: [{
			type: 'main',
			title: '首页',
			url: 'lcxm_main.html'
		}]
	});
});

function checkLogin(){
	var token = storage.get("token");
	if(!token){
		location.href = project_prefix + "lcxm_login.html";
	}
}

//退出登录
function logout(){
	//TODO 调用后台退出接口
	storage.remove("token");
	var url ="/logout";
	$request.post(url);
	location.href = project_prefix + "lcxm_login.html";
}

function initMenus() {
	//简单修改下 支持未来元素
	$('nav').on('click', " .nav-item-has-subnav > a", function() {
		$subnavToggle = jQuery(this);
		$navHasSubnav = $subnavToggle.parent();
		$topHasSubNav = $subnavToggle.parents('.nav-item-has-subnav').last();
		$subnav = $navHasSubnav.find('.nav-subnav').first();
		$viSubHeight = $navHasSubnav.siblings().find('.nav-subnav:visible').outerHeight();
		$scrollBox = $('.lyear-layout-sidebar-scroll');
		$navHasSubnav.siblings().find('.nav-subnav:visible').slideUp(500).parent().removeClass('open');
		$subnav.slideToggle(300, function() {
			$navHasSubnav.toggleClass('open');

			// 新增滚动条处理
			var scrollHeight = 0;
			pervTotal = $topHasSubNav.prevAll().length,
				boxHeight = $scrollBox.outerHeight(),
				innerHeight = $('.sidebar-main').outerHeight(),
				thisScroll = $scrollBox.scrollTop(),
				thisSubHeight = $(this).outerHeight(),
				footHeight = 121;

			if (footHeight + innerHeight - boxHeight >= (pervTotal * 48)) {
				scrollHeight = pervTotal * 48;
			}
			if ($subnavToggle.parents('.nav-item-has-subnav').length == 1) {
				$scrollBox.animate({
					scrollTop: scrollHeight
				}, 300);
			} else {
				// 子菜单操作
				if (typeof($viSubHeight) != 'undefined' && $viSubHeight != null) {
					scrollHeight = thisScroll + thisSubHeight - $viSubHeight;
					$scrollBox.animate({
						scrollTop: scrollHeight
					}, 300);
				} else {
					if ((thisScroll + boxHeight - $scrollBox[0].scrollHeight) == 0) {
						scrollHeight = thisScroll - thisSubHeight;
						$scrollBox.animate({
							scrollTop: scrollHeight
						}, 300);
					}
				}
			}
		});
	});

}
//加载菜单
function loadMenus() {
	var url = "/system/menu/menuTree"
	var menuVm;
	$request.get(url, {
			error: function(jqXHR, textStatus, errorMsg) { // 出错时时候加载测试菜单
				console.error('发送AJAX请求到"' + this.url + '"时出错[' + jqXHR.status + ']：' + errorMsg);
				demoMenus = {"code":0,"msg":"","data":[{"id":2,"createTime":"2019-11-19  20:49:01","updateTime":"2019-11-19  20:50:13","enable":true,"code":101,"name":"开发管理","icon":"fa fa-code","pid":0,"type":1,"url":null,"permission":null,"sort":8,"checked":null,"relId":null,"children":[{"id":10,"createTime":"2019-12-04  18:10:37","updateTime":"2020-01-16  01:32:05","enable":true,"code":101002,"name":"开发计划","icon":"glyphicon glyphicon-list-alt","pid":2,"type":2,"url":"lcxm_develop_plan.html","permission":"","sort":2,"checked":null,"relId":null,"children":[{"id":15,"createTime":"2020-01-20  17:11:54","updateTime":"2020-01-20  17:21:50","enable":true,"code":101002004,"name":"删除","icon":"","pid":10,"type":3,"url":"","permission":"develop.plan.delete","sort":5,"checked":null,"relId":null,"children":[],"root":false,"pnode":false},{"id":14,"createTime":"2020-01-20  01:40:32","updateTime":"2020-01-20  17:21:50","enable":true,"code":101002003,"name":"详情","icon":"","pid":10,"type":3,"url":"","permission":"develop.plan.detail","sort":3,"checked":null,"relId":null,"children":[],"root":false,"pnode":false},{"id":13,"createTime":"2020-01-16  01:39:35","updateTime":"2020-01-16  02:02:30","enable":true,"code":101002002,"name":"新增","icon":"","pid":10,"type":3,"url":"","permission":"develop.plan.add","sort":2,"checked":null,"relId":null,"children":[],"root":false,"pnode":false},{"id":12,"createTime":"2020-01-16  01:32:56","updateTime":"2020-01-16  02:02:30","enable":true,"code":101002001,"name":"列表","icon":"","pid":10,"type":3,"url":"","permission":"develop.plan.list","sort":1,"checked":null,"relId":null,"children":[],"root":false,"pnode":false}],"root":false,"pnode":true},{"id":3,"createTime":"2019-11-19  20:54:16","updateTime":"2019-11-19  20:54:16","enable":true,"code":101001,"name":"代码生成","icon":null,"pid":2,"type":2,"url":"lcxm_other_generator.html","permission":null,"sort":1,"checked":null,"relId":null,"children":[],"root":false,"pnode":false}],"root":true,"pnode":true},{"id":1,"createTime":"2019-11-19  20:41:58","updateTime":"2019-11-19  20:42:44","enable":true,"code":100,"name":"系统管理","icon":"fa fa-cog fa-fw","pid":0,"type":1,"url":null,"permission":null,"sort":9,"checked":null,"relId":null,"children":[{"id":11,"createTime":"2019-12-30  18:42:05","updateTime":"2020-01-16  01:32:05","enable":true,"code":100005,"name":"系统用户","icon":"","pid":1,"type":2,"url":"lcxm_system_user.html","permission":"","sort":3,"checked":null,"relId":null,"children":[],"root":false,"pnode":false},{"id":9,"createTime":"2019-12-01  01:04:19","updateTime":"2020-01-16  01:32:05","enable":true,"code":100004,"name":"角色管理","icon":"","pid":1,"type":2,"url":"lcxm_system_role.html","permission":"","sort":5,"checked":null,"relId":null,"children":[],"root":false,"pnode":false},{"id":6,"createTime":"2019-11-26  02:00:19","updateTime":"2019-11-28  18:57:01","enable":true,"code":100003,"name":"数据字典","icon":"glyphicon glyphicon-book","pid":1,"type":2,"url":"lcxm_system_dict.html","permission":"","sort":4,"checked":null,"relId":null,"children":[{"id":8,"createTime":"2019-11-29  22:29:10","updateTime":"2020-01-16  01:32:05","enable":true,"code":100003001,"name":"新增","icon":"","pid":6,"type":3,"url":"","permission":"system:dict:add","sort":2,"checked":null,"relId":null,"children":[],"root":false,"pnode":false}],"root":false,"pnode":true},{"id":5,"createTime":"2019-11-22  21:03:03","updateTime":"2019-11-22  21:35:31","enable":true,"code":100002,"name":"系统菜单","icon":null,"pid":1,"type":2,"url":"lcxm_system_menu.html","permission":null,"sort":3,"checked":null,"relId":null,"children":[],"root":false,"pnode":false},{"id":4,"createTime":"2019-11-19  22:07:21","updateTime":"2019-11-19  22:07:21","enable":true,"code":100001,"name":"系统用户","icon":null,"pid":1,"type":2,"url":"lcxm_system_user.html","permission":null,"sort":1,"checked":null,"relId":null,"children":[],"root":false,"pnode":false}],"root":true,"pnode":true}]};
				console.info(demoMenus);
				menuVm = new Vue({
					el: 'nav.sidebar-main',
					data: demoMenus
				});
				initMenus();
			}
		},
		function(result) {
			menuVm = new Vue({
				el: 'nav.sidebar-main',
				data: result
			});
			initMenus();
		}
	);
}
